package com.example.myproject.client;
import java.util.ArrayList;
import java.util.Date;
import com.example.myproject.client.entities.Note;
import com.example.myproject.client.widget.TextAreaP;
import com.example.myproject.client.widget.TextFieldP;
import com.example.myproject.shared.TimeException;
import com.gargoylesoftware.htmlunit.OnbeforeunloadHandler;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
public class ObjectifyExample implements EntryPoint {
// Create some fields for the UI
FlowPanel mainPanel = new FlowPanel();
TextFieldP tfChild1 = new TextFieldP();
TextFieldP tfChild2 = new TextFieldP();
TextAreaP taChild3 = new TextAreaP();
TextFieldP tfParent = new TextFieldP();
Label lblResult = new Label();
Button submit = new Button("Submit");
Button search = new Button("Search");
FlexTable searchResultsTable = new FlexTable();
DateTimeFormat sdf = DateTimeFormat.getFormat("EEE, MM/d/yy HH:mm");
// Instantiate the interfaces to access methods in the interface
private final PersistentServiceAsync persistentService = GWT
.create(PersistentService.class);
private final VerticalPanel userPanel = new VerticalPanel();
private final VerticalPanel verticalPanel = new VerticalPanel();
private DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);
public void onModuleLoad() {
// vpSearch.setSpacing(5);
// mainPanel.setSpacing(5);
searchResultsTable.setStyleName("FlexTable");
RootPanel.get("container").add(mainPanel);
mainPanel.add(userPanel);
userPanel.add(tfParent);
tfParent.setPlaceholder("Parent");
tfParent.addKeyPressHandler(new KeyPressHandler() {
@Override
public void onKeyPress(KeyPressEvent event) {
if (event.getCharCode() == KeyCodes.KEY_ENTER) {
getNotes();
}
}
});
userPanel.add(search);
search.addClickHandler(new MyClickHandler("search"));
userPanel.add(tfChild1);
tfChild1.setPlaceholder("Child1");
userPanel.add(tfChild2);
tfChild2.setPlaceholder("Child2");
userPanel.add(taChild3);
taChild3.setPlaceholder("Child3");
userPanel.add(submit);
submit.addClickHandler(new MyClickHandler("submit"));
mainPanel.add(verticalPanel);
mainPanel.add(lblResult);
mainPanel.add(searchResultsTable);
Button btnDebug = new Button("Debug");
btnDebug.addClickHandler(new MyClickHandler("debug"));
mainPanel.add(btnDebug);
getNotes();
}
private class MyClickHandler implements ClickHandler {
private String name;
public MyClickHandler(String name) {
this.name = name;
}
@Override
public void onClick(ClickEvent event) {
if (name.equals("search")) {
getNotes();
} else if (name.equals("submit")) {
if (tfChild1.getText().trim().isEmpty()) {
lblResult.setText("Child1 is blank");
} else {
putNote();
}
} else if (name.equals("debug")) {
String[] data = null;
final int left = event.getClientX();
final int top = event.getClientY();
persistentService.debugInfo(data, new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
setLblResult("Debug RPC call failed ");
}
@Override
public void onSuccess(String result) {
simplePopup.setPopupPosition(0, top);
simplePopup.setWidget(new HTML(result));
simplePopup.setHeight("80px");
simplePopup.setWidth("340px");
// Show the popup
simplePopup.show();
// setLblResult(result);
}
});
}
}
}
private void setLblResult(String msg) {
lblResult.setText(msg);
}
private void putNote() {
Note a = new Note(tfParent.getText().trim(), tfChild1.getText().trim(),
new Date());
persistentService.persistNote(a, new AsyncCallback<Void>() {
@Override
public void onFailure(Throwable caught) {
waitTime(caught);
}
@Override
public void onSuccess(Void result) {
setLblResult("Note stored successfully");
}
});
}
private void getNotes() {
persistentService.searchNote(tfParent.getText(),
new AsyncCallback<ArrayList<Note>>() {
@Override
public void onFailure(Throwable caught) {
waitTime(caught);
}
public void onSuccess(ArrayList<Note> result) {
setLblResult(result.size() + " entities found");
int row = 0;
searchResultsTable.removeAllRows();
searchResultsTable.setText(0, 0, "Date");
searchResultsTable.setText(0, 1, "Note");
// loop the array list and user getters to add
// records to the table
for (Note note : result) {
row = searchResultsTable.getRowCount();
searchResultsTable.setText(row, 0, note.getChild1());
searchResultsTable.setHTML(row, 1,
sdf.format(note.getDate()));
}
}
});
}
private void waitTime(Throwable caught) {
String result = "PersistentService RPC call failed ";
if (caught instanceof TimeException) {
int timeToWait = ((TimeException) caught).getTime();
result = "You must wait " + timeToWait + " seconds.";
}
setLblResult(result);
}
}